#include "enroll.h"
#include "ui_enroll.h"
#include "login.h"


#include<QMessageBox>
#include<QSqlQuery>//执行 SQL语句 的类，包括查询、插入、更新、删除等数据库操作
#include<QSqlDatabase>//管理 数据连接、数据库驱动 的类
#include <QSqlError>

#include<QDebug>
#include<QKeyEvent>


enroll::enroll(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::enroll)
{
    ui->setupUi(this);



    this->setWindowTitle("工业生产检测系统");

    //图标
    ui->userna_pic->setPixmap(QPixmap(":/res/username.png"));
    ui->passw_pic->setPixmap(QPixmap(":/res/password.png"));
    ui->confirm_passw_pic->setPixmap(QPixmap(":/res/password.png"));
    //按比例缩放内容
    ui->userna_pic->setScaledContents(true);
    ui->passw_pic->setScaledContents(true);
    ui->confirm_passw_pic->setScaledContents(true);

    ui->user_Edit->setPlaceholderText("请输入注册账户");
    ui->passw_Edit->setPlaceholderText("请输入密码");
    ui->confirm_passw_Edit->setPlaceholderText("请再次输入密码");

    ui->passw_Edit->setEchoMode(QLineEdit::Password);
    ui->confirm_passw_Edit->setEchoMode(QLineEdit::Password);

    ui->passw_Edit->setMaxLength(16);
    ui->confirm_passw_Edit->setMaxLength(16);






    // 注册按钮
    connect(ui->regist_btn, &QPushButton::clicked, this,&enroll::handleEnroll);

}

enroll::~enroll()
{
    delete ui;
}


void enroll::on_exit_btn_clicked()
{
    login* to_login = new login();
    to_login->show();
    this->hide();
}

void enroll::handleEnroll(){
    QString account = ui->user_Edit->text();
    QString password = ui->passw_Edit->text();
    QString confirm_password = ui->confirm_passw_Edit->text();

    if (password != confirm_password) {
            QMessageBox::critical(this, "注册", "注册失败：两次输入密码不一致");
            return;
        }


    QSqlQuery checkQuery;
        checkQuery.prepare("SELECT * FROM user WHERE account = ?");
        checkQuery.addBindValue(account);
        if (checkQuery.exec() && checkQuery.next()) {
            QMessageBox::information(this, "注册", "该用户已经存在");
        } else {
            QSqlQuery insertQuery;
            insertQuery.prepare("INSERT INTO user (account, password) VALUES (?, ?)");
            insertQuery.addBindValue(account);
            insertQuery.addBindValue(password);

            if (insertQuery.exec()) {
                QMessageBox::information(this, "注册", "注册成功");
            } else {
                QMessageBox::critical(this, "注册", "注册失败：" + insertQuery.lastError().text());
            }
        }

        }

//void enroll::keyReleaseEvent(QKeyEvent *event)
//{
//    switch(event->key()){
//    case Qt::Key_Return:
//        handleEnroll();
//        break;
//    case Qt::Key_Enter:
//        handleEnroll();
//        break;
//    default:
//        break;
//    }
//}
void  enroll::keyReleaseEvent(QKeyEvent *event){
    switch (event->key()) {
    case Qt::Key_Return:
       handleEnroll();
        break;
    case Qt::Key_Enter:
        handleEnroll();
        break;
    default:
        break;
    }
}
//void login::on_conceal_checkBox_clicked(bool checked)
//{   //enum EchoMode { Normal, NoEcho, Password, PasswordEchoOnEdit };
//    if(checked){
//        ui->passw_edit->setEchoMode(QLineEdit::Normal);
//    }else{
//        ui->passw_edit->setEchoMode(QLineEdit::Password);
//    }
//}
void enroll::on_showPasswcheckBox_clicked(bool checked)
{
    //enum EchoMode { Normal, NoEcho, Password, PasswordEchoOnEdit };
        if(checked){
            ui->passw_Edit->setEchoMode(QLineEdit::Normal);
        }else{
            ui->passw_Edit->setEchoMode(QLineEdit::Password);
        }
}

void enroll::on_showConPasswcheckBox_clicked(bool checked)
{
    //enum EchoMode { Normal, NoEcho, Password, PasswordEchoOnEdit };
        if(checked){
            ui->confirm_passw_Edit->setEchoMode(QLineEdit::Normal);
        }else{
            ui->confirm_passw_Edit->setEchoMode(QLineEdit::Password);
        }
}
